/* 
 * Distributed as part of jdiff
 * 
 * Copyright (C) 2008 QArks.com
 *
 * Author: Pierre Meyer <support@qarks.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 2.1, as 
 * published by the Free Software Foundation.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; see the file LICENSE.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package diff.tela;

import java.awt.*;
import javax.swing.*;


import diff.misc.*;

public class FileOverview extends JPanel {

	private ParsedFile parsedFile = null;
	private boolean showNoMatchAsDeleted;

	public FileOverview(boolean showNoMatchAsDeleted) {
		this.showNoMatchAsDeleted = showNoMatchAsDeleted;
	}

	public void setParsedFile(ParsedFile parsedFile) {
		this.parsedFile = parsedFile;
		repaint();
	}

	public void paintComponent(Graphics g) {
		Dimension dim = getSize();
		g.setColor(Color.white);
		g.fillRect(0, 0, dim.width, dim.height);
		int lastY = 0;
		if (parsedFile != null) {
			Font font = getFont();
			FontMetrics fm = getFontMetrics(font);
			int lineHeight = fm.getHeight();

			FileLine[] lines = parsedFile.getLines();
			if (lines != null && lines.length > 0) {
				int w = dim.width;
				float hFloat = dim.height;
				float lCountFloat = lines.length;
				int hLine = (int) (hFloat / lCountFloat);
				int hLineUsed = Math.max(hLine, 3);
				hLineUsed = Math.min(hLineUsed, lineHeight);

				for (int i = 0; i < lines.length; i++) {
					if (lines[i].getIndex() > -1) {
						int yLine;
						if (hLineUsed == lineHeight) {
							// avoid using the whole bar height when no scrollbar is visible
							yLine = (int) (i * lineHeight);
						} else {
							yLine = (int) (((float) dim.height) -
									((float) (lines.length - i)) *
									hFloat /
									lCountFloat);
						}

						FileLine line = lines[i];
						boolean display = false;
						Color color = Color.white;
						if (line.getStatus() == FileLine.NO_MATCH) {
							if (showNoMatchAsDeleted) {
								color = LineCellRenderer.INSERTED_COLOR;
							} else {
								color = LineCellRenderer.DELETED_COLOR;
							}
							display = true;
						} else if (line.getStatus() == FileLine.MOVED) {
							color = LineCellRenderer.MOVED_COLOR;
							display = true;
						} else if (line.getStatus() == FileLine.MODIFIED) {
							color = LineCellRenderer.MODIFIED_COLOR;
							display = true;
						} else if (line.getStatus() == FileLine.DELETED_ON_OTHER_SIDE) {
							color = LineCellRenderer.DELETED_COLOR;
							display = true;
						} else if (line.getStatus() == FileLine.INSERTED_ON_OTHER_SIDE) {
							color = LineCellRenderer.INSERTED_COLOR;
							display = true;
						}


						if (display) {
							/*
                            if (lastY < (yLine - 1) && (hLineUsed < lineHeight)) {
                                // some glitches due to approximations.. if new starting line index is more than 1 pixel
                                // lower than previous while item height is less than a line height, we have to fill the gap
                                // with the last color used
                                for (int z = 0; z < (yLine - 1 - lastY); z++) {
                                    g.drawLine(0, yLine - z - 1, w, yLine - z - 1);
                                }
                            }*/
							g.setColor(color);
							for (int z = 0; z < dim.height && z <= hLineUsed; z++) {
								g.drawLine(0, yLine + z, w, yLine + z);
							}
						}

						lastY = yLine + hLineUsed - 1;
					}
				}
			}
		} else {
			super.paintComponent(g);
		}
	}
}
